<template>
	<view>
		<view class="search-1">
			<uni-search-bar @input="input" @clear="clear" cancelButton="none" :radius="30"></uni-search-bar>
		</view>
		<view class="history-1" v-if="searchlist.length==0">
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="cleardata"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag class='uni-tag' inverted="true" :text='item' v-for="(item,index) in historycom" :key='index'
					@click="gogoodslist(item)">
				</uni-tag>
			</view>
		</view>
		<view class="search-list" v-else='searchlist.length!==0'>
			<view class="search-item" @click="gogoods_derail(item.goods_id)" v-for="(item,index) in searchlist"
				:key='index'>
				<view class="okk">{{item.goods_name}}</view>
				<uni-icons type="right"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		searchdata
	} from '../../api/search_request.js'
	export default {
		data() {
			return {
				timer: null,
				keyword: '',
				searchlist: [],
				historylist: [],
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('keyword') || '[]');
		},
		computed: {
			historycom() {
				return [...this.historylist].reverse();
			}
		},
		methods: {
			input(e) {
				// console.log(e);
				clearTimeout(this.timer);
				this.timer = setTimeout(() => {
					console.log(e);
					this.keyword = e;
					this.getsearch();
					this.saveaaa();
				}, 1000)
			},
			async getsearch() {
				if (this.keyword === '') {
					this.searchlist = [];
					return
				}
				const {
					data: res
				} = await searchdata(this.keyword);
				console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg();
				this.searchlist = res.message
			},
			gogoods_derail(goods_id) {
				uni.navigateTo({
					url: '/ok/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},
			clear() {
				console.log(11);
				this.searchlist = []
			},
			// 保存搜索的关键字
			saveaaa() {
				// this.historylist.push(this.keyword)
				const set = new Set(this.historylist);
				set.delete(this.keyword);
				set.add(this.keyword);
				this.historylist = Array.from(set);
				uni.setStorageSync('keyword', JSON.stringify(this.historyList));
			},
			cleardata() {
				this.historylist = [];
				uni.setStorageSync('keyword', '[]');
			},
			gogoodslist(keyword) {
				uni.navigateTo({
					url: '/ok/goods_list/goods_list?query=' + keyword
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-list {
		padding: 0 10px;

		.search-item {
			padding: 10px 0;
			display: flex;
			justify-content: center;
			align-items: center;
			border-bottom: 1px solid gray;

			.okk {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.history-1 {
		padding: 0 10px;

		.history-title {
			display: flex;
			justify-content: space-between;
			height: 40px;
			border-bottom: 1px solid gray;
		}

		.history-list {
			margin-top: 10px;
			display: flex;
			flex-wrap: wrap;

		}

		.uni-tag {
			margin: 5px;
		}
	}
</style>
